﻿/*
 * RegExpress
 * 
 * Copyright (c) 2010, Daniel McGaughran
 * 
 * Licensed under the Apache Licence, Version 2.0 (the "Licence");
 * you may not use this file except in compliance with the Licence.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 * 
 */

using System;
using RegExpressModel.Entities;


namespace RegExpressModel
{
	public class ReplaceMatchesModel : BaseRegexModel
	{
		private string _input;
		private string _replacementExpression;



		public ReplaceMatchesModel()
        {
            IsValid = false;
        }



		public string Input
		{
			get { return _input; }
			set
			{
				_input = value;
				ResetResults();
			}
		}


		public string ReplacementExpression
		{
			get { return _replacementExpression; }
			set
			{
				_replacementExpression = value;
				ResetResults();
			}
		}


		public MatchResultType ResultStatus { get; private set; }
		public string Output { get; private set; }



		public bool PerformReplacement()
		{
			if(IsValid)
			{
				ResultStatus = (RegexObject.IsMatch(Input))?
						MatchResultType.Matches : MatchResultType.NoMatch;
				Output = RegexObject.Replace(Input, ReplacementExpression);
				return true;
			}

			return false;
		}



		protected override void ResetResults()
		{
			ResultStatus = MatchResultType.Unknown;
			Output = "";
		}

	
	}
}
